package com.zhang.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;

/**
 * @author ZhangJunqi
 * @Date 2022/3/17 -20:40
 */
@Configuration
@EnableSwagger2
@Profile({"dev","test"})
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
//登录:   http://localhost:8080/swagger-ui.html
public class Swagger2Config {
    //配置Swagger的Docket Bean实例

    @Bean
    public Docket docketA(){
        return new Docket (DocumentationType.SWAGGER_2).groupName ("A");
    }

    @Bean
    public Docket docketB(){
        return new Docket (DocumentationType.SWAGGER_2).groupName ("B");
    }

    @Bean
    public Docket docket(Environment environment){
        //需求：设置要显示Swagger的环境
        Profiles profiles=Profiles.of ("dev","test");
        boolean b=environment.acceptsProfiles (profiles);
        //注意端口号改变了
        System.out.println(b);
        return new Docket (DocumentationType.SWAGGER_2)
                .apiInfo (apiInfo ())
                //配置API文档分组
                .groupName ("老张")
                //是否启动Swagger  false则不能再浏览器访问
                .enable (b)
                .select ()
                //指定要扫描的包RequestHandlerSelectors.basePackage ("com.zhang.controller")
                //RequestHandlerSelectors.any ()扫描全部     .none()全不扫描
                //.withClassAnnotation (RestController.class)扫描类上的注解    参数是注解的反射对象
                // .withMethodAnnotation (Annotation)    扫描方法上的注解
                .apis (RequestHandlerSelectors.basePackage ("com.zhang.controller"))
                //过滤路径PathSelectors.ant("/zhang/**")  .none()不过滤  .any()过滤全部
                .paths (PathSelectors.any ())
                .build ();
    }

    //配置ApiInfo基本信息
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "老张的Swagger API文档",
                "描述：付出多少努力==赚多少钱",
                "v1.0", "https://maven.apache.org/",
                new Contact ("老张", "https://gitee.com/zhangjunqistudy", "https://user.qzone.qq.com/1726684834?source=namecardhoverqzone"),
                "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList ());
    }
}
